package com.hziee.cosmanage.companymanage.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.hziee.common.utils.R;
import com.hziee.cosmanage.companymanage.entity.PartEntity;
import com.hziee.cosmanage.companymanage.feign.MemberFeignService;
import com.hziee.cosmanage.companymanage.service.PartService;
import com.hziee.cosmanage.companymanage.vo.UserCompanyVo;
import com.hziee.cosmanage.companymanage.vo.UserInfoVo;
import org.apache.commons.lang3.AnnotationUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.common.utils.PageUtils;
import com.hziee.common.utils.Query;

import com.hziee.cosmanage.companymanage.dao.UserInfoDao;
import com.hziee.cosmanage.companymanage.entity.UserInfoEntity;
import com.hziee.cosmanage.companymanage.service.UserInfoService;


@Service("userInfoService")
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao, UserInfoEntity> implements UserInfoService {

    @Autowired
    MemberFeignService memberFeignService;

    @Autowired
    PartService partService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserInfoEntity> page = this.page(
                new Query<UserInfoEntity>().getPage(params),
                new QueryWrapper<UserInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<PartEntity> getAllPartUserInfo(Integer companyId) {
        //1.获得所有部门
        List<PartEntity> entities = partService.list();
        //2.获得所有员工信息
        R companyUser = memberFeignService.getAllCompanyUser(companyId);
        List<UserCompanyVo> data = companyUser.getData(new TypeReference<List<UserCompanyVo>>() {
        });
        List<UserInfoVo> collect = data.stream().map(userCompanyVo -> {
            Integer userId = userCompanyVo.getUserId();
            UserInfoVo userInfoVo = new UserInfoVo();
            UserInfoEntity entity = this.getOne(new QueryWrapper<UserInfoEntity>().eq("user_id", userId));
            BeanUtils.copyProperties(entity, userInfoVo);
            userInfoVo.setPartId(userCompanyVo.getCompanyPartId());
            return userInfoVo;
        }).collect(Collectors.toList());
        //3.把所对应的员工信息放到对应的部门下
        List<PartEntity> collect2 = entities.stream().map(partEntity -> {
            List<UserInfoEntity> collect1 = collect.stream().filter(userInfoVo -> {
                return userInfoVo.getPartId() == partEntity.getCompanyPartId();
            }).map(vo -> {
                UserInfoEntity entity = new UserInfoEntity();
                BeanUtils.copyProperties(vo, entity);
                return entity;
            }).collect(Collectors.toList());
            if (collect1.size() == 0) {
                List<UserInfoEntity> infoEntities = new ArrayList<>();
                UserInfoEntity entity = new UserInfoEntity();
                entity.setAge(0);
                infoEntities.add(entity);
                partEntity.setUserInfoEntities(infoEntities);
                return partEntity;
            } else {
                partEntity.setUserInfoEntities(collect1);
                return partEntity;
            }
        }).collect(Collectors.toList());
        return collect2;
    }

}